package com.hzsoft.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hzsoft.base.entity.BJuiJSON;
import com.hzsoft.base.entity.Page;
import com.hzsoft.sys.entity.SysDept;
import com.hzsoft.sys.service.SysDeptService;

@Controller
@RequestMapping("/system/dept")
public class SysDeptController extends com.hzsoft.base.BaseController{
	
	@Autowired
	SysDeptService sysDeptService;
	
	@RequestMapping("/systemDeptList")
	public ModelAndView systemDeptList(@ModelAttribute SysDept dept,Model model,HttpServletRequest request,HttpServletResponse response){
		//分页设置
		String pageCurrent = request.getParameter("pageCurrent");
		Page page =  new Page();
		if (StringUtils.isNotBlank(pageCurrent)) {
			page.setPageCurrent(Integer.parseInt(pageCurrent));
			dept.setPage(page);
		}
		
		if(dept.getName() == null){
			dept.setName("");
		}
		dept.setName(dept.getName().trim());
		List<SysDept> list = sysDeptService.listPageDept(dept);
		
		model.addAttribute("dept",dept);
		model.addAttribute("page", dept.getPage());
		model.addAttribute("list",list);
		
		return new ModelAndView("/system/dept/deptList");
	}
	
	/**
	 * 弹出添加页面
	 * @param menu
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddDept")
	public ModelAndView toAddDept(@ModelAttribute SysDept dept,HttpServletResponse response, HttpServletRequest request){
		ModelAndView model = new ModelAndView();
	
		model.setViewName("/system/dept/addDept");
		return model;
	}
	
	/**
	 * 功能设置：添加
	 */
	@RequestMapping(value = "/addDept")	
	public ModelAndView addDept(SysDept record,HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv=new ModelAndView();
		BJuiJSON bjui = new BJuiJSON();
		SysDept dept = sysDeptService.selectByPrimaryKey(record.getDeptId());
		if(dept!=null){
			bjui.setStatusCode("300");
			bjui.setMessage("该项已存在！！！");
		}else{
			try {
				sysDeptService.insertSelective(record);
				
				bjui.setStatusCode("200");
				bjui.setMessage("添加成功！！！");
				bjui.setTabid("navtab-dept");
				bjui.setCloseCurrent(true);
				
			} catch (Exception e) {
				e.printStackTrace();				
				bjui.setStatusCode("300");
				bjui.setMessage("添加失败！！！");
			}
		}
		String msg = JSONObject.fromObject(bjui).toString();
		mv.addObject("msg", msg);
		mv.setViewName("msg");
		return mv;
	}
	
	/**
	 * 功能设置：删除
	 */
	@RequestMapping(value = "/deleteDept")
	public ModelAndView deleteDept(@ModelAttribute SysDept dept,HttpServletRequest request,HttpServletResponse response) throws IOException{
		BJuiJSON bjui = new BJuiJSON();
		ModelAndView mv = new ModelAndView();
//		System.out.println("---"+dept.getIds());
		
		try {
//			sysDeptService.deleteAll(dept);
			
			sysDeptService.deleteByPrimaryKey(dept.getDeptId());
			
			bjui.setStatusCode("200");
			bjui.setMessage("删除成功！！！");
			bjui.setTabid("navtab-dept");
		} catch (Exception e) {
			e.printStackTrace();
			bjui.setStatusCode("300");
			bjui.setMessage("删除失败！！！");
		}
		
		String msg = JSONObject.fromObject(bjui).toString();
		mv.addObject("msg",msg);
		mv.setViewName("msg");
		return mv;
	}
	
	/**
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/toUpdateDept")
	public ModelAndView toUpdateDept(SysDept dept){
		ModelAndView mv = new ModelAndView();
		mv.addObject("dept", sysDeptService.selectByPrimaryKey(dept.getDeptId()));
		mv.setViewName("system/dept/updateDept");
		return mv;
	}
	
	/**
	 * 
	 * @param dept
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/updateDept",method=RequestMethod.POST)
	public ModelAndView updateDept(SysDept dept,HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		BJuiJSON bjui = new BJuiJSON();
		try {
			sysDeptService.updateByPrimaryKeySelective(dept);
			
			bjui.setStatusCode("200");
			bjui.setMessage("更新成功！！！");
			bjui.setTabid("navtab-dept");
			bjui.setCloseCurrent(true);
			
		} catch (Exception e) {
			bjui.setStatusCode("300");
			bjui.setMessage("更新失败！！！");
		}
		String msg = JSONObject.fromObject(bjui).toString();
		mv.addObject("msg",msg);
		mv.setViewName("msg");
		return mv;
	}
	
}